package com.lyw.login

import androidx.activity.viewModels
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.lu.library.base.BaseActivity
import com.lyw.container.ContainerActivity
import com.lyw.login.databinding.ActivityLoginBinding
import com.lyw.login.databinding.ActivityPhoneLoginBinding
import com.lyw.login.vm.LoginIntent
import com.lyw.login.vm.LoginPhoneIntent
import com.lyw.login.vm.LoginPhoneUiState
import com.lyw.login.vm.LoginPhoneVm
import com.lyw.login.vm.LoginUiState
import com.lyw.login.vm.LoginVm
import com.lyw.login_api.LoginRoutePaths
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = LoginRoutePaths.PATH_LOGIN_PHONE)
@AndroidEntryPoint
class LoginWithPhoneActivity : BaseActivity<ActivityPhoneLoginBinding>() {

    private val loginVm by viewModels<LoginPhoneVm>()
    override fun initViews() {
        super.initViews()

        binding.tvLogin.setOnClickListener {
            loginVm.sendUiIntent(LoginPhoneIntent.LoginIn(binding.etPhone.text.toString(),binding.etCode.text.toString()))
        }
        binding.tvSendCode.setOnClickListener {
            loginVm.sendUiIntent(LoginPhoneIntent.SendCode(binding.etPhone.text.toString()))
        }
    }

    override fun initListener() {
        super.initListener()
        binding.etPhone.addTextChangedListener {
            loginVm.sendUiIntent(LoginPhoneIntent.PhoneChange(it.toString()))
        }
        binding.etCode.addTextChangedListener {
            loginVm.sendUiIntent(LoginPhoneIntent.CodeChange(it.toString()))
        }
    }
    override fun initData() {
        lifecycleScope.launch {

            LogUtils.d("collect start")
            loginVm.uiStateFlow.collect{
                LogUtils.d(Thread.currentThread())
                when(it){
                    is LoginPhoneUiState.INIT ->{
                        binding.tvLogin.isEnabled = it.enableLogin
                        binding.tvSendCode.isEnabled = it.enableCode
                    }
                    is LoginPhoneUiState.LoginSuccess -> {
                        ToastUtils.showShort("登录成功")
                        startActivity(ContainerActivity::class.java)
                    }
                    is LoginPhoneUiState.CountDown -> {
                        binding.tvSendCode.isEnabled = it.enableCode
                        binding.tvSendCode.text = it.str
                    }
                    is LoginPhoneUiState.LoginEnable -> {
                        binding.tvLogin.isEnabled = it.loginEnable
                    }

                    is LoginPhoneUiState.SendCodeEnable -> {
                        binding.tvSendCode.isEnabled = it.codeEnable
                    }
                }
            }

            LogUtils.d("collect done")
        }

    }

    override fun layoutResId(): Int = R.layout.activity_phone_login
}